package com.example.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entity.Intermediator;
import com.example.service.IntermediatorService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lmy
 * @since 2024-08-04
 */
@RestController
@RequestMapping("/jjrperson")
@ApiOperation(value = "中间人操作")
public class IntermediatorController {

    @Autowired
    private IntermediatorService intermediatorService;
//居间人列表查询

    @ApiOperation(value = "分页居间人列表")
    @GetMapping("/pageList/{page}/{limit}")
    public Result jjrpageList(@ApiParam(name = "page", value = "当前页码", required = true)@PathVariable Long page,
                              @ApiParam(name = "limit", value = "每页记录数", required = true)@PathVariable Long limit){
        Page<Intermediator> pageParam=new Page<>(page,limit);
        intermediatorService.page(pageParam,null);
        List<Intermediator > records=pageParam.getRecords();//获取当前页的记录列表
        Long total=pageParam.getTotal();
        return Result.ok().data("total",total).data("rows",records);
    }
    @ApiOperation(value = "新增中间人信息")
    @PostMapping("/addjjr")
    public Result addjjr(@RequestBody Intermediator intermediator){
        boolean id =intermediatorService.save(intermediator);
        if(id){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
    @ApiOperation(value = "删除用户")
    @DeleteMapping("/deletejjr/{id}")
    public Result deletejjr(@PathVariable Long id){
        boolean b=intermediatorService.removeById(id);
        if(b){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
    @ApiOperation(value = "用户信息回写")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id){
        Intermediator intermediator=intermediatorService.getById(id);
        return Result.ok().data("intermediator",intermediator);
    }
    @ApiOperation(value = "修改用户信息")
    @PutMapping("/editjjr")
    public Result editjjr(@RequestBody Intermediator intermediator){
        boolean b=intermediatorService.updateById(intermediator);
        if(b){
            return Result.ok();
        }
        return Result.error();
    }
}
